﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Business.GeneralParameters
{
    public class CurrencyExchangeManager
    {
        public CurrencyExchange CurrencyExchangeTransactionExchange(CurrencyExchange vCurrencyExchange, bool vActionBuy)
        {
            Classifier vMoneyBase;
            Classifier vMoneyIncome;
            Classifier vMoneyExit;
            CurrencyExchangeRates vCurrencyBaseExchangeRate = null;
            CurrencyExchangeRates vCurrencyIncomeExchangeRate = null;
            CurrencyExchangeRates vCurrencyExitExchangeRate = null;

            bool vContinue = true;
            string vMessage = "";

            CommonClassifierManager classifierMgr = new CommonClassifierManager();

            vMoneyBase = classifierMgr.GetClassifierByID(Convert.ToInt64(MoneyEnum.LocalMoney));
            vMoneyIncome = classifierMgr.GetClassifierByID(vCurrencyExchange.IdMoneyIncome);
            vMoneyExit = classifierMgr.GetClassifierByID(vCurrencyExchange.IdMoneyExit);
            if (vMoneyBase == null)
            {
                vContinue = false;
                vMessage = "tre/Currency/NotExistCurrencyExchange";
            }
            if (vMoneyIncome == null || vMoneyIncome.IdClassifier <= 0)
            {
                vContinue = false;
                vMessage = "tre/CurrencyInput/NotExistMoney";
            }
            if (vMoneyExit == null || vMoneyExit.IdClassifier <= 0)
            {
                vContinue = false;
                vMessage = "tre/CurrencyOuput/NotExistMoney";
            }

            if (vContinue)
            {
                if (vCurrencyExchange.ProcessDate == DateTime.MinValue)
                    vCurrencyExchange.ProcessDate = System.DateTime.Today;

                CurrencyExchangeRatesManager ratesMgr = new CurrencyExchangeRatesManager();

                /// Cargar los TC
                vCurrencyBaseExchangeRate =
                    ratesMgr.CurrencyExchangeRatesGetByIdMoneyAndDate(vMoneyBase.IdClassifier, vCurrencyExchange.ProcessDate);
                vCurrencyIncomeExchangeRate =
                   ratesMgr.CurrencyExchangeRatesGetByIdMoneyAndDate(vCurrencyExchange.IdMoneyIncome, vCurrencyExchange.ProcessDate);
                vCurrencyExitExchangeRate =
                    ratesMgr.CurrencyExchangeRatesGetByIdMoneyAndDate(vCurrencyExchange.IdMoneyExit, vCurrencyExchange.ProcessDate);

                /// Validar 
                if (vCurrencyBaseExchangeRate == null)
                {
                    vContinue = false;
                    vMessage = "tre/Currency/NotExistCurrencyExchangeBase";
                }
                if (vCurrencyIncomeExchangeRate == null)
                {
                    vContinue = false;
                    // vMessage = "No se encontro el tipo de cambio para la moneda de ingreso, por lo tanto no se pudo realizar la operacion.";
                    vMessage = "tre/CurrencyExchangeRate/IncomeMoney";
                }
                if (vCurrencyExitExchangeRate == null)
                {
                    vContinue = false;
                    // vMessage = "No se encontro el tipo de cambio para la moneda de salida, por lo tanto no se pudo realizar la operacion.";
                    vMessage = "tre/CurrencyExchangeRate/ExitMoney";
                }

                if (vContinue)
                {

                    //- Datos del cambio de moneda -
                    vCurrencyExchange.MoneyBaseShortDescription = vMoneyBase.Code;
                    vCurrencyExchange.MoneyExitShortDescription = vMoneyExit.Code;
                    vCurrencyExchange.MoneyIncomeShortDescription = vMoneyIncome.Code;
                    vCurrencyExchange.ObjMoneyBase = vMoneyBase;
                    vCurrencyExchange.ObjMoneyExit = vMoneyExit;
                    vCurrencyExchange.ObjMoneyIncome = vMoneyIncome;

                    if (vCurrencyExchange.IdMoneyIncome == vCurrencyBaseExchangeRate.IdcMoney || vCurrencyExchange.IdMoneyExit == vCurrencyBaseExchangeRate.IdcMoney)
                    {
                        //- Tipo de cambio SIMPLE -
                        vCurrencyExchange.AdvancedExchange = false;
                        if (vActionBuy)
                        {
                            #region " Tipo de cambio simple compra "
                            //- Tipo de cambio SIMPLE COMPRA -

                            //- CALCULO -
                            //validamos si tenemos el monto de entrada o el monto de salida
                            if (vCurrencyExchange.AmmountIncome > 0)
                            {
                                vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome;
                                if (vCurrencyExchange.IdMoneyIncome == vMoneyBase.IdClassifier)
                                {
                                    vCurrencyExchange.AmmountExit = Convert.ToDecimal(vCurrencyExchange.AmmountIncome) / Convert.ToDecimal(vCurrencyExitExchangeRate.BuyExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyExitExchangeRate.BuyExchangeRate);
                                }
                                else
                                {
                                    vCurrencyExchange.AmmountExit = Convert.ToDecimal(vCurrencyExchange.AmmountIncome) * Convert.ToDecimal(vCurrencyIncomeExchangeRate.BuyExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyIncomeExchangeRate.BuyExchangeRate);
                                }
                            }
                            else
                            {
                                if (vCurrencyExchange.IdMoneyIncome == vMoneyBase.IdClassifier)
                                {
                                    vCurrencyExchange.AmmountIncome = Convert.ToDecimal(vCurrencyExchange.AmmountExit) * Convert.ToDecimal(vCurrencyExitExchangeRate.BuyExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyExitExchangeRate.BuyExchangeRate);
                                }
                                else
                                {
                                    vCurrencyExchange.AmmountIncome = Convert.ToDecimal(vCurrencyExchange.AmmountExit) / Convert.ToDecimal(vCurrencyIncomeExchangeRate.BuyExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyIncomeExchangeRate.BuyExchangeRate);
                                }
                                vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome;
                            }

                            vCurrencyExchange.CurrencyExchangeRateAdvanced = 0;

                            #endregion
                        }
                        else
                        {
                            #region " Tipo de cambio simple venta "

                            //- Tipo de cambio SIMPLE VENTA -

                            //- CALCULO -
                            vCurrencyExchange.IdMoneyBase = vMoneyBase.IdClassifier;
                            //validamos si tenemos el monto de entrada o el monto de salida
                            if (vCurrencyExchange.AmmountIncome > 0)
                            {
                                vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome;
                                if (vCurrencyExchange.IdMoneyIncome == vMoneyBase.IdClassifier)
                                {
                                    vCurrencyExchange.AmmountExit = Convert.ToDecimal(vCurrencyExchange.AmmountIncome) / Convert.ToDecimal(vCurrencyExitExchangeRate.SellExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyExitExchangeRate.SellExchangeRate);
                                }
                                else
                                {
                                    vCurrencyExchange.AmmountExit = Convert.ToDecimal(vCurrencyExchange.AmmountIncome) * Convert.ToDecimal(vCurrencyIncomeExchangeRate.SellExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyIncomeExchangeRate.SellExchangeRate);
                                }
                            }
                            else
                            {
                                if (vCurrencyExchange.IdMoneyIncome == vMoneyBase.IdClassifier)
                                {
                                    vCurrencyExchange.AmmountIncome = Convert.ToDecimal(vCurrencyExchange.AmmountExit) * Convert.ToDecimal(vCurrencyExitExchangeRate.SellExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyExitExchangeRate.SellExchangeRate);
                                }
                                else
                                {
                                    vCurrencyExchange.AmmountIncome = Convert.ToDecimal(vCurrencyExchange.AmmountExit) / Convert.ToDecimal(vCurrencyIncomeExchangeRate.SellExchangeRate);
                                    vCurrencyExchange.CurrencyExchangeRate = Convert.ToDecimal(vCurrencyIncomeExchangeRate.SellExchangeRate);
                                }
                                vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome;
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        //- Tipo de cambio AVANZADO -
                        vCurrencyExchange.AdvancedExchange = true;

                        #region " Tipo de cambio avanzado fase de venta "

                        //- Tipo de cambio AVANZADO -

                        //- CALCULO -
                        vCurrencyExchange.IdMoneyBase = vMoneyBase.IdClassifier;
                        if (vCurrencyExchange.AmmountIncome > 0)
                        {
                            //transformamos a moneda base desde el monto de entrada
                            vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome * vCurrencyIncomeExchangeRate.BuyExchangeRate;
                            vCurrencyExchange.CurrencyExchangeRate = vCurrencyIncomeExchangeRate.BuyExchangeRate;

                            //ahora transformamos a moneda de salida
                            vCurrencyExchange.AmmountExit = vCurrencyExchange.AmmountBase / vCurrencyExitExchangeRate.SellExchangeRate;
                            vCurrencyExchange.CurrencyExchangeRateAdvanced = vCurrencyExitExchangeRate.SellExchangeRate;
                        }
                        else
                        {
                            //transformamos a moneda base desde el monto de salida
                            vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountExit * vCurrencyExitExchangeRate.SellExchangeRate;
                            vCurrencyExchange.CurrencyExchangeRateAdvanced = vCurrencyExitExchangeRate.SellExchangeRate;

                            //ahora transformamos a moneda de entrada
                            vCurrencyExchange.AmmountIncome = vCurrencyExchange.AmmountBase / vCurrencyIncomeExchangeRate.BuyExchangeRate;
                            vCurrencyExchange.CurrencyExchangeRate = vCurrencyIncomeExchangeRate.BuyExchangeRate;
                        }

                        #endregion

                    }
                }
                else
                {
                    throw new Exception(vMessage);
                }
            }
            else
            {
                throw new Exception(vMessage);
            }
            vCurrencyExchange.IsCurrencyBuy = vActionBuy;
            return vCurrencyExchange;
        }

        /// <summary>
        /// Retorana una entidad con los datos de cambio de moneda.
        /// </summary>
        /// <param name="vCurrencyExchange">Entidad con los datos para el tipo de cambio.</param>
        /// <returns></returns>
        /// <remarks>
        /// No se graba ninguna transaccion, se realiza un cambio de moneda de forma informativa con el tipo de cambio contable, esto lo usa el modulo de facturacion.
        /// </remarks>
        /// <change who="Kevin Guisbert" when="20080213" what="Creacion"/>
        public CurrencyExchange CurrencyExchangeGetExchange(CurrencyExchange vCurrencyExchange)
        {
            CommonClassifierManager classifierMgr = new CommonClassifierManager();

            Classifier vMoneyBase;
            Classifier vMoneyIncome;
            Classifier vMoneyExit;
            CurrencyExchangeRates vCurrencyBaseExchangeRate;
            CurrencyExchangeRates vCurrencyIncomeExchangeRate;
            CurrencyExchangeRates vCurrencyExitExchangeRate;
            bool vContinue = true;
            string vMessage = "";

            vMoneyBase = classifierMgr.GetClassifierByID(Convert.ToInt64(MoneyEnum.LocalMoney));
            vMoneyIncome = classifierMgr.GetClassifierByID(vCurrencyExchange.IdMoneyIncome);
            vMoneyExit = classifierMgr.GetClassifierByID(vCurrencyExchange.IdMoneyExit);
            if (vMoneyBase == null)
            {
                vContinue = false;
                vMessage = "tre/Currency/NotExistCurrencyExchange";
            }
            if (vMoneyIncome == null || vMoneyIncome.IdClassifier <= 0)
            {
                vContinue = false;
                vMessage = "tre/CurrencyInput/NotExistMoney";
            }
            if (vMoneyExit == null || vMoneyExit.IdClassifier <= 0)
            {
                vContinue = false;
                vMessage = "tre/CurrencyOuput/NotExistMoney";
            }

            if (vContinue)
            {
                if (vCurrencyExchange.ProcessDate == DateTime.MinValue)
                    vCurrencyExchange.ProcessDate = System.DateTime.Today;

                CurrencyExchangeRatesManager ratesMgr = new CurrencyExchangeRatesManager();

                /// Cargar los TC
                vCurrencyBaseExchangeRate =
                    ratesMgr.CurrencyExchangeRatesGetByIdMoneyAndDate(vMoneyBase.IdClassifier, vCurrencyExchange.ProcessDate);
                vCurrencyIncomeExchangeRate =
                   ratesMgr.CurrencyExchangeRatesGetByIdMoneyAndDate(vCurrencyExchange.IdMoneyIncome, vCurrencyExchange.ProcessDate);
                vCurrencyExitExchangeRate =
                    ratesMgr.CurrencyExchangeRatesGetByIdMoneyAndDate(vCurrencyExchange.IdMoneyExit, vCurrencyExchange.ProcessDate);
                if (vCurrencyBaseExchangeRate == null)
                {
                    vContinue = false;
                    vMessage = "tre/Currency/NotExistCurrencyExchangeBase";
                }
                if (vCurrencyIncomeExchangeRate == null)
                {
                    vContinue = false;
                    vMessage = "tre/CurrencyExchangeRate/IncomeMoney";
                }
                if (vCurrencyExitExchangeRate == null)
                {
                    vContinue = false;
                    vMessage = "tre/CurrencyExchangeRate/ExitMoney";
                }
                if (vContinue)
                {
                    //- Datos del cambio de moneda -
                    vCurrencyExchange.MoneyBaseShortDescription = vMoneyBase.Code;
                    vCurrencyExchange.MoneyExitShortDescription = vMoneyExit.Code;
                    vCurrencyExchange.MoneyIncomeShortDescription = vMoneyIncome.Code;
                    vCurrencyExchange.ObjMoneyBase = vMoneyBase;
                    vCurrencyExchange.ObjMoneyExit = vMoneyExit;
                    vCurrencyExchange.ObjMoneyIncome = vMoneyIncome;

                    if (vCurrencyExchange.IdMoneyIncome == vCurrencyBaseExchangeRate.IdcMoney || vCurrencyExchange.IdMoneyExit == vCurrencyBaseExchangeRate.IdcMoney)
                    {
                        //- Tipo de cambio SIMPLE -
                        vCurrencyExchange.AdvancedExchange = false;
                        #region " Tipo de cambio simple"

                        vCurrencyExchange.IdMoneyBase = vMoneyBase.IdClassifier;
                        vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome;

                        if (vCurrencyExchange.IdMoneyIncome == vMoneyBase.IdClassifier)
                        {
                            vCurrencyExchange.AmmountExit = vCurrencyExchange.AmmountIncome / vCurrencyExitExchangeRate.BuyExchangeRate;
                            vCurrencyExchange.CurrencyExchangeRate = vCurrencyExitExchangeRate.BuyExchangeRate;
                        }
                        else
                        {
                            vCurrencyExchange.AmmountExit = vCurrencyExchange.AmmountIncome * vCurrencyIncomeExchangeRate.BuyExchangeRate;
                            vCurrencyExchange.CurrencyExchangeRate = vCurrencyIncomeExchangeRate.BuyExchangeRate;
                        }

                        vCurrencyExchange.CurrencyExchangeRateAdvanced = 0;
                        #endregion
                    }
                    else
                    {
                        //- Tipo de cambio AVANZADO -
                        vCurrencyExchange.AdvancedExchange = true;

                        #region " Tipo de cambio avanzado fase de venta "

                        //- CALCULO -
                        vCurrencyExchange.IdMoneyBase = vMoneyBase.IdClassifier;

                        vCurrencyExchange.AmmountBase = vCurrencyExchange.AmmountIncome * vCurrencyIncomeExchangeRate.BuyExchangeRate;
                        vCurrencyExchange.CurrencyExchangeRate = vCurrencyIncomeExchangeRate.BuyExchangeRate;

                        #endregion

                        #region " Tipo de cambio avanzado fase compra "

                        //- Tipo de cambio AVANZADO -

                        //- CALCULO -
                        vCurrencyExchange.AmmountExit = vCurrencyExchange.AmmountBase / vCurrencyExitExchangeRate.BuyExchangeRate;
                        vCurrencyExchange.CurrencyExchangeRateAdvanced = vCurrencyExitExchangeRate.BuyExchangeRate;
                        #endregion
                    }
                }
                else
                {
                    throw new Exception(vMessage);
                }
            }
            else
            {
                throw new Exception(vMessage);
            }
            return vCurrencyExchange;
        }
    }
}
